package idiet.dao.hibernate;

import idiet.bean.Meal;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("mealDao")
public class MealDao extends BaseDao {
	public Meal getMealById(int mealId){
		return (Meal) this.sessionFactory
				.getCurrentSession()
				.get(Meal.class, mealId);
				/*.createCriteria(Meal.class)
				.add(Restrictions.eq("id", mealId))
				.uniqueResult();*/
	}
	/*public Meal getMeal(int id){
		return (Meal) this.sessionFactory.getCurrentSession()
				.get(Meal.class, id);
	}*/
	@SuppressWarnings("unchecked")
	public List<Meal> getMealsByMealName(String name){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Meal.class)
				.add(Restrictions.like("name",name))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<Meal> getMeals(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Meal.class)
				.list();
	}
	public void createMeal(Meal meal){
		this.sessionFactory
		.getCurrentSession()
		.save(meal);
	}
	public void updateMeal(Meal meal){
		this.sessionFactory
		.getCurrentSession()
		.update(meal);
	}
	public void deleteMeal(Meal meal){
		this.sessionFactory
		.getCurrentSession()
		.delete(meal);
	}
}
